package cn.th.service.impl;

import cn.th.dto.AdDto;
import cn.th.dto.RouteDto;
import cn.th.entity.AdvLabel;
import cn.th.entity.Advertising;
import cn.th.entity.Route;
import cn.th.entity.RouteImage;
import cn.th.mapper.RouteImageMapper;
import cn.th.mapper.RouteMapper;
import cn.th.service.IRouteImageService;
import cn.th.service.IRouteService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 路线流程表 服务实现类
 * </p>
 *
 * @author cn.th
 * @since 2024-03-30
 */
@Service
public class RouteServiceImpl extends ServiceImpl<RouteMapper, Route> implements IRouteService {

    @Autowired
    private IRouteImageService routeImageService;

    @Autowired
    private IRouteService routeService;

    @Autowired
    private RouteMapper routeMapper;

    @Autowired
    private RouteImageMapper routeImageMapper;

    /**
     * 保存信息到路线流程表和关联的图片表
     * @param routeDto
     */
    @Override
    public void saveRouteDto(RouteDto routeDto) {
        //保存路线的基本信息到路线流程表route
        this.save(routeDto);

        Long routeId = routeDto.getId();//广告id

        //图片
        List<RouteImage> routeImageList = routeDto.getImg();
        routeImageList = routeImageList.stream().map((item) -> {
            item.setRouteId(routeId);
            return item;
        }).collect(Collectors.toList());

        //保存标签数据到标签表route_image
        routeImageService.saveBatch(routeImageList);
    }

    /**
     * 查询
     * @param page
     * @param pageSize
     * @param school
     * @param name
     * @param location
     * @return
     */
    @Override
    public Map<String, Object> queryRoute(int page, int pageSize, String school, String name, String location) {
        LambdaQueryWrapper<Route> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.hasLength(school), Route::getUserSchool, school);
        wrapper.eq(StringUtils.hasLength(name), Route::getName, name);
        wrapper.eq(StringUtils.hasLength(location), Route::getLocation, location);

        Page<Route> page1 = new Page<>(page,pageSize);
        Page<RouteDto> routeDtoPage = new Page<>();

        routeService.page(page1,wrapper);

        //对象拷贝
        BeanUtils.copyProperties(page1,routeDtoPage,"records");

        List<Route> records = page1.getRecords();

        List<RouteDto> list = records.stream().map((item) -> {
            RouteDto routeDto = new RouteDto();

            BeanUtils.copyProperties(item,routeDto);

            //当前路线的id
            Long routeId = item.getId(); // 路线id

            LambdaQueryWrapper<RouteImage> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(RouteImage::getRouteId,routeId);

            List<RouteImage> routeImageList = routeImageService.list(lambdaQueryWrapper);
            routeDto.setImg(routeImageList);
            return routeDto;
        }).collect(Collectors.toList());

        routeDtoPage.setRecords(list);

        Map<String, Object> data = new HashMap<>();
        data.put("total",routeDtoPage.getTotal());
        data.put("rows",routeDtoPage.getRecords());

        return data;
    }

    /**
     * 删除相关信息
     * @param ids
     */
    @Override
    @Transactional
    public void removeRouteById(String ids) {
        routeMapper.deleteBatchByIds(ids);
        routeImageMapper.deleteBatchByIds(ids);
    }

    /**
     * 更新相关信息
     * @param routeDto
     */
    @Override
    @Transactional
    public void updateRouteById(RouteDto routeDto) {
        //更新广告表基本信息
        this.updateById(routeDto);

        //清理当前广告对应标签---route_image表的delete操作
        LambdaQueryWrapper<RouteImage> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(RouteImage::getRouteId,routeDto.getId());
        routeImageService.remove(queryWrapper);

        //添加当前提交过来标签数据---route_image表的insert操作
        List<RouteImage> advLabelList = routeDto.getImg();

        advLabelList = advLabelList.stream().map((item) -> {
            item.setRouteId(routeDto.getId());
            return item;
        }).collect(Collectors.toList());

        routeImageService.saveBatch(advLabelList);
    }

    /**
     * 修改路线时页面的数据展示
     * @param id
     * @return
     */
    @Override
    @Transactional
    public RouteDto getRouteDtoById(Long id) {
        //查询路线基本信息，从Advertising表查询
        Route route = this.getById(id);

        RouteDto routeDto = new RouteDto();
        BeanUtils.copyProperties(route,routeDto);

        //查询当前路线表对应的图片，从route_image查询
        LambdaQueryWrapper<RouteImage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RouteImage::getRouteId,route.getId());
        List<RouteImage> routeImageList = routeImageService.list(queryWrapper);
        routeDto.setImg(routeImageList);

        return routeDto;
    }
}
